class Article < ActiveRecord::Base
  has_many :comments, :dependent => :destroy, :order => 'created_at'
  belongs_to :user
  
  acts_as_taggable
  attr_writer :tag_list

  acts_as_textiled :body
  
  def after_save
    self.tag_with @tag_list
  end
  
  validates_presence_of :title, :body

  # find a random article
  # URL: /articles/random, /articles/random.xml etc.
  # http://weblog.jamisbuck.org/2006/10/7/helping-activerecord-finders-help-you
  def self.find(*args)
    if args.first.to_s == "random"
      ids = connection.select_all("SELECT id FROM articles")
      super(ids[rand(ids.length)]["id"].to_i)
    else
      super
    end
  end
end
